package com.ms.biz.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.system.comm.utils.request.ReqBodyParamsFilter;

/**
 * WebMvc的配置
 * @author yuejing
 * @date 2019年3月5日 下午1:15:08
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		/*// 添加拦截器
		registry.addInterceptor(new AuthSecurityInterceptor())
		.addPathPatterns("/**")
		.excludePathPatterns("/index.html", "/error", "/static/**");
		WebMvcConfigurer.super.addInterceptors(registry);*/
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
		WebMvcConfigurer.super.addResourceHandlers(registry);
	}
	
	@Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        //setUseSuffixPatternMatch 后缀模式匹配
        configurer.setUseSuffixPatternMatch(true);
        //setUseTrailingSlashMatch 自动后缀路径模式匹配
        configurer.setUseTrailingSlashMatch(true);
    }
	
	/**
	 * 处理请求参数为json字符串的过滤器
	 * @return
	 */
	@Bean
    public FilterRegistrationBean<ReqBodyParamsFilter> someFilterRegistration() {
        FilterRegistrationBean<ReqBodyParamsFilter> registration = new FilterRegistrationBean<ReqBodyParamsFilter>();
        registration.setFilter(reqBodyParamsFilter());
        registration.addUrlPatterns("/*");
        registration.setName("ReqBodyParamsFilter");
        return registration;
    }
    /**
     * request json body 过滤器
     * @return
     */
    @Bean(name = "reqBodyParamsFilter")
    public ReqBodyParamsFilter reqBodyParamsFilter() {
        return new ReqBodyParamsFilter();
    }
}
